package com.bing.bingPro.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.controller.dto.TZdIndexMonthDto;
import com.bing.bingPro.controller.vo.TZdIndexMonthVo;
import com.bing.bingPro.entity.TZdIndexMonth;
import com.bing.bingPro.mapper.TZdIndexMonthMapper;
import com.bing.bingPro.service.TZdIndexMonthService;
import com.bing.bingPro.util.DateConversion;
import com.bing.bingPro.util.ProvinceUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author wangqilong
 * @description 针对表【t_zd_index_month】的数据库操作Service实现
 * @createDate
 */
@Service
public class TZdIndexMonthServiceImpl extends ServiceImpl<TZdIndexMonthMapper, TZdIndexMonth>
        implements TZdIndexMonthService {

    @Override
    public List<TZdIndexMonthVo> tZdIndexMonthList(TZdIndexMonthDto tZdIndexMonthDto) {
        String province = tZdIndexMonthDto.getProvince();
        String time = tZdIndexMonthDto.getTime().replace("-", "");
        List<TZdIndexMonth> tZdIndexMonths = baseMapper.selectList(Wrappers.lambdaQuery(TZdIndexMonth.class)
                .eq(CharSequenceUtil.isNotEmpty(time), TZdIndexMonth::getDt, time)
                .eq(CharSequenceUtil.isNotEmpty(province), TZdIndexMonth::getProvinceCode, province));
        Set<String> provinceCodes = tZdIndexMonths.stream()
                .map(TZdIndexMonth::getProvinceCode)
                .collect(Collectors.toSet());

        if (CharSequenceUtil.isNotEmpty(province)) {
            provinceCodes.add("CN");
            List<TZdIndexMonth> extraCNData = baseMapper.selectList(Wrappers.lambdaQuery(TZdIndexMonth.class)
                    .eq(CharSequenceUtil.isNotEmpty(time), TZdIndexMonth::getDt, time)
                    .eq(TZdIndexMonth::getProvinceCode, "CN"));

            tZdIndexMonths.addAll(extraCNData);
        }
        //获取装维数据
        List<TZdIndexMonthVo> list = baseMapper.selectInstallationNo(time);
        Map<String, Integer> provinceToInstallationNoMap = list.stream()
                .collect(Collectors.toMap(TZdIndexMonthVo::getProvinceCode, TZdIndexMonthVo::getInstallationNo));
        //获取集团数据
        List<TZdIndexMonthVo> allRateList = baseMapper.selectRate(tZdIndexMonthDto.getTime());
        Map<String, TZdIndexMonthVo> provinceToRateMap = allRateList.stream()
                .collect(Collectors.toMap(TZdIndexMonthVo::getProvinceCode, Function.identity(), (oldValue, newValue) -> oldValue));

        return tZdIndexMonths.stream().map(tZdIndexMonth -> {
            TZdIndexMonthVo vo = new TZdIndexMonthVo();
            BeanUtils.copyProperties(tZdIndexMonth, vo);
            vo.setDt(tZdIndexMonthDto.getTime());
            vo.setProvinceCode(getProvince(tZdIndexMonth.getProvinceCode()));
            vo.setSuptOrderNum(tZdIndexMonth.getSuptOrderNum() != null ? transformation(tZdIndexMonth.getSuptOrderNum()) : null);
            vo.setInstallationNo(provinceToInstallationNoMap.get(tZdIndexMonth.getProvinceCode()));
            vo.setAcceptOrderAvgTime(tZdIndexMonth.getAcceptOrderAvgTime() != null ? reservation(tZdIndexMonth.getAcceptOrderAvgTime()) : null);
            vo.setHandleOrderAvgTime(tZdIndexMonth.getHandleOrderAvgTime() != null ? reservation(tZdIndexMonth.getHandleOrderAvgTime()) : null);
            if (tZdIndexMonth.getProvZdPeopleNum() != null &&
                    tZdIndexMonth.getCityZdPeopleNum() != null &&
                    (tZdIndexMonth.getProvZdPeopleNum() + tZdIndexMonth.getCityZdPeopleNum() != 0) &&
                    vo.getInstallationNo() != null) {
                vo.setSupportZdPeopleNum(vo.getInstallationNo() / (tZdIndexMonth.getProvZdPeopleNum() + tZdIndexMonth.getCityZdPeopleNum()));
            }
            vo.setHundredZwExpertRate(tZdIndexMonth.getExpertNum() != null &&
                    vo.getInstallationNo() != null ?
                    BigDecimal.valueOf((double) tZdIndexMonth.getExpertNum() * 100 / vo.getInstallationNo())
                            .setScale(4, RoundingMode.HALF_UP).doubleValue() : null);
            vo.setHundredZwRate(tZdIndexMonth.getSuptPeopleNum() != null &&
                    vo.getInstallationNo() != null ?
                    BigDecimal.valueOf((double) tZdIndexMonth.getSuptPeopleNum() * 100 / vo.getInstallationNo())
                            .setScale(4, RoundingMode.HALF_UP).doubleValue() : null);
            TZdIndexMonthVo rateVo = provinceToRateMap.get(ProvinceUtil.getProvince(tZdIndexMonth.getProvinceCode()));
            if (rateVo != null) {
                vo.setInternetComplain(rateVo.getInternetComplain());
                vo.setNetworkSatisfaction(rateVo.getNetworkSatisfaction());
                vo.setInstallSatisfaction(rateVo.getInstallSatisfaction());
                vo.setRepairSatisfaction(rateVo.getRepairSatisfaction());
                vo.setComplainResolveRate(rateVo.getComplainResolveRate());
                vo.setQualityRate(rateVo.getQualityRate());
                vo.setComplainTimelyRate(rateVo.getComplainTimelyRate());
            }
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 单位变为万,四舍五入保留两位小数
     *
     * @param value
     * @return
     */
    private Double transformation(Integer value) {
        return (double) Math.round(value * 100 / 10000.0) / 100;
    }

    /**
     * 四舍五入保留两位小数
     *
     * @param value
     * @return
     */
    private BigDecimal reservation(BigDecimal value) {
        return value.setScale(2, RoundingMode.HALF_UP);
    }

    @Override
    public List<String> dateList() {
        return baseMapper.selectDate().stream()
                .map(DateConversion::convertToYYYYMM)
                .collect(Collectors.toList());
    }


    public static String getProvince(String code) {
        final Map<String, String> provinceNameMap = new LinkedHashMap<>();
        provinceNameMap.put("AH", "安徽");
        provinceNameMap.put("BJ", "北京");
        provinceNameMap.put("CQ", "重庆");
        provinceNameMap.put("FJ", "福建");
        provinceNameMap.put("GD", "广东");
        provinceNameMap.put("GS", "甘肃");
        provinceNameMap.put("GX", "广西");
        provinceNameMap.put("GZ", "贵州");
        provinceNameMap.put("HA", "河南");
        provinceNameMap.put("HB", "湖北");
        provinceNameMap.put("HE", "河北");
        provinceNameMap.put("HI", "海南");
        provinceNameMap.put("HL", "黑龙江");
        provinceNameMap.put("HN", "湖南");
        provinceNameMap.put("JL", "吉林");
        provinceNameMap.put("JS", "江苏");
        provinceNameMap.put("JX", "江西");
        provinceNameMap.put("LN", "辽宁");
        provinceNameMap.put("NM", "内蒙古");
        provinceNameMap.put("NX", "宁夏");
        provinceNameMap.put("QH", "青海");
        provinceNameMap.put("SC", "四川");
        provinceNameMap.put("SD", "山东");
        provinceNameMap.put("SH", "上海");
        provinceNameMap.put("SN", "陕西");
        provinceNameMap.put("SX", "山西");
        provinceNameMap.put("TJ", "天津");
        provinceNameMap.put("XJ", "新疆");
        provinceNameMap.put("XZ", "西藏");
        provinceNameMap.put("YN", "云南");
        provinceNameMap.put("ZJ", "浙江");
        provinceNameMap.put("CN", "全网");
        if (!provinceNameMap.containsKey(code)) {
            return null;
        }
        return provinceNameMap.get(code);
    }
}




